#!/bin/bash
set -e

# felt: a command-line utility for building and testing Flutter web engine.
#       It stands for Flutter Engine Local Tester.

FELT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )"

if [ -z "`which gclient`" ]
then
  echo "ERROR: gclient is not in your PATH"
  echo "Fix: add the path to your installation of depot_tools to your PATH"
  exit 1
fi
GCLIENT_PATH=`which gclient`

if [ -z "`which ninja`" ]
then
  echo "ERROR: ninja is not in your PATH"
  echo "Fix: add the path to your installation of depot_tools to your PATH"
  exit 1
fi
NINJA_PATH=`which ninja`

ENGINE_SRC_DIR="$(dirname $(dirname $(dirname $(dirname ${FELT_DIR}))))"
FLUTTER_DIR="${ENGINE_SRC_DIR}/flutter"
WEB_UI_DIR="${FLUTTER_DIR}/lib/web_ui"
DEV_DIR="${WEB_UI_DIR}/dev"
OUT_DIR="${ENGINE_SRC_DIR}/out"
HOST_DEBUG_UNOPT_DIR="${ENGINE_SRC_DIR}/out/host_debug_unopt"
DART_SDK_DIR="${ENGINE_SRC_DIR}/out/host_debug_unopt/dart-sdk"
GN="${FLUTTER_DIR}/tools/gn"
DART_TOOL_DIR="${WEB_UI_DIR}/.dart_tool"
PUB_PATH="$DART_SDK_DIR/bin/pub"
SNAPSHOT_PATH="${DART_TOOL_DIR}/felt.snapshot"
STAMP_PATH="${DART_TOOL_DIR}/felt.snapshot.stamp"
SCRIPT_PATH="${DEV_DIR}/felt.dart"
REVISION="$(cd "$FLUTTER_DIR"; git rev-parse HEAD)"

if [ ! -f "${PUB_PATH}" ]
then
  echo "Compiling the Dart SDK."
  gclient sync
  $GN --unoptimized --full-dart-sdk
  ninja -C $HOST_DEBUG_UNOPT_DIR -j 100
fi

install_deps() {
  echo "Running \`pub get\` in 'engine/src/flutter/lib/web_ui'"
  (cd "$WEB_UI_DIR"; $PUB_PATH get)

  echo "Running \`pub get\` in 'engine/src/flutter/web_sdk/web_engine_tester'"
  (cd "$FLUTTER_DIR/web_sdk/web_engine_tester"; $PUB_PATH get)
}

KERNEL_NAME=`uname`
if [[ $KERNEL_NAME == *"Darwin"* ]]
then
  echo "Running on MacOS. Will check the file and user limits."
  # Disable exit if the commands fails. We want to give more actionable
  # error message.
  set +e
  ULIMIT_FILES=`ulimit -n`
  # Increase the file limit if it is low. Note that these limits are changed
  # only for this script (for this shell). After felt execution is completed
  # no change is required to reset the original shell.
  if [[ $ULIMIT_FILES < 50000 ]]
  then
    echo "File limits too low increasing the file limits"
    # Get the max file limit.
    MAX_ULIMIT_FILES=`launchctl limit maxfiles | sed -e 's/^[[:space:]]*//' | sed 's/ \{1,\}/ /g' | cut -d' ' -f2`
    if [[ $MAX_ULIMIT_FILES < 50000 ]]
    then
      # Increase the maximum file limit.
      sudo launchctl limit maxfiles 50000 200000
    fi
    ERROR=$(ulimit -n 50000 2>&1 >/dev/null)
    if [[ ! -z $ERROR ]]
    then
      echo "Problem changing the file limit. Please try to reboot to use the higher limits. error: \n$ERROR" 1>&2
    fi
  fi
  ULIMIT_USER=`ulimit -u`
  # Increase the hard user limit if it is lower than 2048.
  if [[ $ULIMIT_USER < 4096 ]]
  then
    echo "User limits too low increasing the user limits"
    ERROR2=$(ulimit -u 4096 2>&1 >/dev/null)
    if [[ ! -z $ERROR2 ]]
    then
      echo "Problem changing the user limit. Please try to reboot to use the higher limits. error: \n$ERROR2" 1>&2
    fi
  fi
  # Set the value back to exit on non zero results.
  set -e
fi

if [[ "$FELT_USE_SNAPSHOT" == "false" || "$FELT_USE_SNAPSHOT" == "0" ]]; then
  echo "[Snapshot mode: off]"
  # Running without snapshot means there is high likelyhood of local changes. In
  # that case, let's clear the snapshot to avoid any surprises.
  rm -f "$SNAPSHOT_PATH"
  rm -f "$STAMP_PATH"
  install_deps
  $DART_SDK_DIR/bin/dart "$DEV_DIR/felt.dart" $@
else
  # Create a new snapshot if any of the following is true:
  #  * SNAPSHOT_PATH is not a file, or
  #  * STAMP_PATH is not a file with nonzero size, or
  #  * Contents of STAMP_PATH is not our local git HEAD revision, or
  #  * pubspec.yaml last modified after pubspec.lock
  if [[ ! -f $SNAPSHOT_PATH || ! -s "$STAMP_PATH" || "$(cat "$STAMP_PATH")" != "$REVISION" || "$WEB_UI_DIR/pubspec.yaml" -nt "$WEB_UI_DIR/pubspec.lock" ]]; then
    echo "[Snapshot mode: on] (creating a new snapshot)"
    install_deps
    mkdir -p $DART_TOOL_DIR

    "$DART_SDK_DIR/bin/dart" --snapshot="$SNAPSHOT_PATH" --packages="$WEB_UI_DIR/.packages" "$SCRIPT_PATH"
    echo "$REVISION" > "$STAMP_PATH"
  fi

  $DART_SDK_DIR/bin/dart --packages="$WEB_UI_DIR/.packages" "$SNAPSHOT_PATH" $@
fi
